var app = getApp();
var getSign = require('../../utils/getSign/util.js')
Page({
  data: {

    radioItems: [
      { name: '男', value: '1', checked: 'true'  },
      { name: '女', value: '2', checked: ''},
     
    ],
    hidden: false,
    date: '请选择',
    Logo:'',
    MemberPhone:'',
    MemberEmail:'',
    Member_Birthday:'',
    Nike_Name:'',
    Sex:0

  },
  changelogo: function(){
    //上传图片
      var that = this;
      wx.chooseImage({
        count: 1,  //最多可以选择的图片总数
        sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths;
          console.log("上传服务器前");
          console.log(res);
          console.log(res.tempFilePaths[0]);
          var path = res.tempFilePaths[0];
          //启动上传等待中...
          wx.showToast({
            title: '正在上传...',
            icon: 'loading',
            mask: true,
            duration: 10000
          })
          var imgtype = "";
          if (path.indexOf("/") > 0)//如果包含有"/"号 从最后一个"/"号+1的位置开始截取字符串
          {
            imgtype = path.substring(path.lastIndexOf("/") + 1, path.length);
          }
          that.setData({
            Logo: path
          })
          wx.uploadFile({
            url: 'https://wx-app-api.sjune.cn/yuren/FileUpload.ashx',
            filePath: tempFilePaths[0],
            name: 'FileData',
            formData: {
              'fileName': imgtype
            },
            header: {
              "Content-Type": "multipart/form-data"
            },
            success: function (res) {
//服务器返回格式: { "Catalog": "testFolder", "FileName": "1.jpg", "Url": "https://test.com/1.jpg" }
              wx.hideToast();
              if (res.statusCode == 200) {
                console.log("上传服务器后res.data");
                console.log(res.data);

                var obj = JSON.parse(res.data); //由JSON字符串转换为JSON对象
                if (obj.status == 1) {
                  that.setData({
                    Logo: obj.path
                  })
                }
              }
            },
            fail: function (res) {
              wx.hideToast();
              wx.showModal({
                title: '错误提示',
                content: '上传图片失败',
                showCancel: false,
                success: function (res) { }
              })
            }
          });
        }
      });

  },
  changeName: function (e){
    var name = e.detail.value
    this.setData({
      Nike_Name: name
    })
  },
  changePhone: function (e) {
    this.setData({
      MemberPhone: e.detail.value
    })
  },
  changeEmail: function (e) {
    this.setData({
      MemberEmail: e.detail.value
    })
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value,
      Member_Birthday: e.detail.value
    })
  },
  radioChange: function (e) {

    var checked = e.detail.value
    if (checked == '男'){
     this.setData({
       Sex:1
     })
    } else if (checked == '女'){
      this.setData({
        Sex: 2
      })
    }

    var changed = {}
    for (var i = 0; i < this.data.radioItems.length; i++) {
      if (checked.indexOf(this.data.radioItems[i].name) !== -1) {
        changed['radioItems[' + i + '].checked'] = true
      } else {
        changed['radioItems[' + i + '].checked'] = false
      }
    }
    this.setData(changed)
  },

    /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //没有数据 请求(apitype, pageIndex, classId, keys)
    var that = this
    this.requestPost('023', that)
  },
  //提交修改
  submitToNet: function(){
    var memberInfo = wx.getStorageSync('memberInfo')
    var postArgs = {
      'Api_Type': '024',
      'Logo': this.data.Logo,
      'Nike_Name': this.data.Nike_Name,
      'Member_Phone': this.data.MemberPhone,
      'Member_Email': this.data.MemberEmail,
      'Sex': this.data.Sex,
      'Member_Birthday': this.data.Member_Birthday,
      'Member_Id': memberInfo.MemberId,
      'Member_Token': memberInfo.MemberToken
    }
    //获取
    getSign.request('https://wx-app-api.sjune.cn/yuren/API.ashx', 'POST', postArgs, '').then(
      (res) => {
        console.log(res, '修改成功')

        wx.showToast({
          title: '保存成功',
          duration: 2000,
          mask:true,
          success:()=>{
            setTimeout(()=>{
              wx.navigateBack({
                delta: 1
              })
            },2000)
          }
        })
      },
      (err) => {
        console.log(err.Message, '失败')
      }
    )
  },
  //发送请求
  requestPost: function (apitype, that) {
    var memberInfo = wx.getStorageSync('memberInfo')
    var postArgs = {
      'Api_Type': apitype,
      'Member_Id': memberInfo.MemberId,
      'Member_Token': memberInfo.MemberToken
    }
    //获取
    getSign.request('https://wx-app-api.sjune.cn/yuren/API.ashx', 'POST', postArgs, '').then(
      (res) => {
        console.log(res, '个人信息')
        if (res.Status == 2) {
          wx.clearStorage({
            success: () => {
              wx.switchTab({
                url: '/pages/index/index',
              })
            }
          })
        } else if (res.Status == 0) {
          this.setData({
            Logo: res.Data.Logo,
            Member_Birthday: res.Data.Member_Birthday,
            Nike_Name: res.Data.Nike_Name,
            Sex: res.Data.Sex
          })
          if (res.Data.Member_Phone == null) {
            this.setData({
              MemberPhone: '',
            })
          } else {
            this.setData({
              MemberPhone: res.Data.Member_Phone,
            })
          }
          if (res.Data.Member_Email == null) {
            this.setData({
              MemberEmail: '',
            })
          } else {
            this.setData({
              MemberEmail: res.Data.Member_Email,
            })
          }
          var sexcheck = []
          if (res.Data.Sex == 1) {
            sexcheck = [
              { name: '男', value: '1', checked: 'true' },
              { name: '女', value: '2' }
            ]
            this.setData({
              radioItems: sexcheck
            })
          } else if (res.Data.Sex == 2) {
            sexcheck = [
              { name: '男', value: '1' },
              { name: '女', value: '2', checked: 'true' }
            ]
            this.setData({
              radioItems: sexcheck
            })
          }

          if (res.Data.Member_Birthday != 'null' && res.Data.Member_Birthday != null) {
            this.setData({
              date: res.Data.Member_Birthday
            })
          }
        }
        

      },
      (err) => {
        console.log(err.Message, '成功列表失败')
      }
    )
  },
})